package com.huaxindata.im.main.menu;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.huaxin.common.BaseFragment;
import com.huaxindata.im.R;
import com.huaxindata.im.R2;
import com.huaxindata.im.main.contactview.friend.ContactInfoDialog;
import com.huaxindata.im.main.contactview.friend.HxPersonalCenterActivity;
import com.huaxindata.im.main.contactview.friend.HxSearchContactAdapter;
import com.huaxindata.im.model.contact.Contact;
import com.huaxindata.im.model.contact.ContactPerson;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author songxf
 * @date 2017/11/8
 * @Desc 搜索Fragment
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class HxSearchContactFragment extends BaseFragment {
    @BindView(R2.id.recycler)
    RecyclerView mRecycler;
    private HxSearchContactAdapter mContactAdapter;
    List<Contact> mContactList = new ArrayList<>();
    private View mNoDataView;

    public static HxSearchContactFragment newInstance() {

        Bundle args = new Bundle();

        HxSearchContactFragment fragment = new HxSearchContactFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_search_contact;
    }

    @Override
    protected void initView(View view) {
        mRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecycler.setHasFixedSize(true);
        mContactAdapter = new HxSearchContactAdapter();
        mRecycler.setAdapter(mContactAdapter);

        //空界面
        mNoDataView = LayoutInflater.from(getContext()).inflate(R.layout.static_no_data, (ViewGroup) mRecycler.getParent(), false);
        mNoDataView.findViewById(R.id.no_data_ll).setVisibility(View.VISIBLE);
        mContactAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Contact contact = mContactAdapter.getData().get(position);
               /* ContactInfoDialog contactInfoDialog = new ContactInfoDialog(getActivity());
                contactInfoDialog.setOwnerActivity(getActivity());
                contactInfoDialog.show(contact);*/

                Intent intent = new Intent(getActivity(), HxPersonalCenterActivity.class);
                Bundle bundle = new Bundle();
                bundle.putParcelable("contact", contact);
                intent.putExtras(bundle);
                getActivity().startActivity(intent);
            }
        });
    }

    public void setSearchData(ContactPerson contactPerson) {
        if (!TextUtils.isEmpty(contactPerson.getNickName())) {
            mContactList.add(contactPerson);
            mContactAdapter.setNewData(mContactList);
        } else {
            mContactAdapter.setEmptyView(mNoDataView);
        }

    }

}
